package com.jiang.demo.springbootmultidatasources.dsconfig.properties;

import com.zaxxer.hikari.HikariDataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class DSConfig {

    @Autowired
    private DB1Property db1Property;

    @Autowired
    private DB2Property db2Property;

    @Bean(name = "db1DS")
    @Primary
    public DataSource db1DS(){
        log.info("db1DS初始化...");
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(db1Property.getUrl());
        ds.setUsername(db1Property.getUsername());
        ds.setPassword(db1Property.getPassword());
        ds.setDriverClassName(db1Property.getDriverClassName());
        return ds;
    }

    @Bean(name = "db2DS")
    public DataSource db2DS(){
        log.info("db2DS初始化...");
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(db2Property.getUrl());
        ds.setUsername(db2Property.getUsername());
        ds.setPassword(db2Property.getPassword());
        ds.setDriverClassName(db2Property.getDriverClassName());
        return ds;
    }
}
